from django import template

register = template.Library()

def items_in_cart_js(cart, category=None):
    """
    Return JS slug/qty pairs for items in shopping cart
    """
    if cart:
        items = cart.cartitem_set.all()
        if category:
            items = items.filter(product__category=category)
        qtys = [u"'%s':%d" % (item.product.slug, item.quantity) for item in items]
        return u','.join(qtys)
    else: 
        return u''

register.simple_tag(items_in_cart_js)
